import json
from django.http import JsonResponse
from django.http import HttpResponse
from django.shortcuts import render
from django.shortcuts import redirect
import pandas as pd
import os
import requests as rep
import base64


#你好，此代码
def login(requests):
    if requests.method == "GET":
        return render(requests, 'login.html')
    username = requests.POST['username']
    print('用户名：',username)
    pwd = requests.POST['pwd']
    print('密码：', pwd)

    tbl_datas = pd.read_excel('t_user.xlsx')
    datas = list(tbl_datas.loc[:, 'username'])
    if username in datas:
        i = datas.index(username)
        real_name = tbl_datas.loc[i, 'names']
        passwd = tbl_datas.loc[i, 'password']
        if pwd == str(passwd):
            requests.session["realname"] = real_name
            requests.session["username"] = username
            requests.session["ls_login"] = True
            datas = {"code": 101, "msg": "登录正确", "data": []}
        else:
            datas = {"code":103,"msg":"密码错误，请重新输入","data":[]}
    else:
        datas = {"code":102,"msg":"用户名不存在，请重新输入","data":[]}
    return JsonResponse(datas)



def showIndex(requests):
    login_status = requests.session.get("ls_login")
    print("状态", login_status)
    real_name = requests.session.get("username")
    print("真是姓名",real_name)
    if not login_status:
        return redirect('/login/')
    return render(requests,'index.html',{"real_name":real_name})

def showSimple(requests):
    return render(requests, 'simple.html')

def receives(requests):
    username = requests.POST["username"]
    pwd = requests.POST["pwd"]
    print("用户名：",username)
    print("密码：",pwd)
    datas = {"statu":1,"msg":"login success","data":[12,45,46,9988]}
    return HttpResponse(json.dumps(datas),content_type="application/json")

def recongnize(requests):
    typeRadio = int(requests.POST["typeRadio"])
    image = requests.FILES.get("image",None)
    if image is None:
        datas = {"statu":411,"msg":"file upload fail","data":[12]}
        return HttpResponse(json.dumps(datas), content_type="application/json")
    else:
        dest = open(os.path.join("./upload/",image.name),"wb+")
        for chunk in image.chunks():
            dest.write(chunk)
        dest.close()
    request_url = ""
    if typeRadio == 1:
        request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/animal"
    elif typeRadio == 2:
        request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/plant"
    elif typeRadio == 3:
        request_url = " "
    elif typeRadio == 4:
        request_url = " "
    elif typeRadio == 5:
        request_url = " "
    else:
        requests_url = ""

    # 二进制方式打开图片所属的[本地文件]
    f = open(os.path.join("./upload",image.name), 'rb')
    img = base64.b64encode(f.read())

    params = {"image": img}
    access_token = '24.0c922eb5b895f75c4446067a9f60dfa0.2592000.1687401787.282335-27138527'
    request_url = request_url + "?access_token=" + access_token
    headers = {'content-type': 'application/x-www-form-urlencoded'}
    response = rep.post(request_url, data=params, headers=headers)
    if response:
        print(response.json())

    datas = {"statu":1,"msg":"login success","data":[12,45,46,9988]}
    return HttpResponse(json.dumps(datas),content_type="application/json")















def showTest(requests):

    return render(requests,'test.html')

def receive(requests):
    username = requests.POST["username"]
    pwd = requests.POST["pwd"]
    print("用户名：",username)
    print("密码：",pwd)
    return HttpResponse("你好")